﻿using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using ZedGraph;

namespace CalcSharp
{
    public partial class plot : Form
    {
        private int minpos, maxpos;
        private bool visibleoptions;
        public plot()
        {
            InitializeComponent();
            this.minpos = slider.Height - slider.Panel2MinSize - slider.SplitterWidth;
            this.maxpos = slider.Height - 85 - slider.SplitterWidth;
        }
        public void Curve(PointPairList list, string title)
        {
            this.btnClose.Enabled = true;
            GraphPane myPane = graph.GraphPane;
            LineItem myCurve = myPane.AddCurve( title, list, Color.Red, SymbolType.None );
            graph.AxisChange();
        }

        public void Pie(double[] its)
        {
            GraphPane myPane = graph.GraphPane;
            string[] itst = new string[its.Length];
            for (int i = 0; i < its.Length; i++) itst[i] = its[i].ToString();
            myPane.AddPieSlices(its, itst);
            graph.AxisChange();
        }

        public void Bar(PointPairList list, string title)
        {
            GraphPane myPane = graph.GraphPane;
            myPane.AddBar(title, list, Color.Red);
            graph.AxisChange();
        }

        private void plot_FormClosed(object sender, FormClosedEventArgs e)
        {
            (Program.loaded.Parent as Parentform).redrawstatusbar();
        }

        private AxisType string2axist(string text)
        {
            if (text.ToUpper(CultureInfo.CurrentCulture) == "DATE") return AxisType.Date;
            else if (text.ToUpper(CultureInfo.CurrentCulture) == "DATE AS ORDINAL") return AxisType.DateAsOrdinal;
            else if (text.ToUpper(CultureInfo.CurrentCulture) == "EXPONENT") return AxisType.Exponent;
            else if (text.ToUpper(CultureInfo.CurrentCulture) == "LINEAR") return AxisType.Linear;
            else if (text.ToUpper(CultureInfo.CurrentCulture) == "LINEAR AS ORDINAL") return AxisType.LinearAsOrdinal;
            else if (text.ToUpper(CultureInfo.CurrentCulture) == "LOG") return AxisType.Log;
            else if (text.ToUpper(CultureInfo.CurrentCulture) == "ORDINAL") return AxisType.Ordinal;
            else if (text.ToUpper(CultureInfo.CurrentCulture) == "TEXT") return AxisType.Text;
            else return AxisType.Linear;
        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.minpos = slider.Height - slider.Panel2MinSize - slider.SplitterWidth;
            this.maxpos = slider.Height - 85 - slider.SplitterWidth;
            if (slider.SplitterDistance == this.minpos)
            {
                slider.SplitterDistance = this.maxpos;
                this.visibleoptions = true;
            }
            else
            {
                this.visibleoptions = false;
                this.slider.SplitterDistance = this.minpos;
            }
        }

        private void XaxisType_SelectedIndexChanged(object sender, EventArgs e)
        {
            AxisType t = string2axist(XaxisType.SelectedItem.ToString());
            graph.GraphPane.XAxis.Type = t;
            graph.AxisChange();
            graph.Refresh();
        }

        private void YaxisType_SelectedIndexChanged(object sender, EventArgs e)
        {
            AxisType t = string2axist(YaxisType.SelectedItem.ToString());
            graph.GraphPane.YAxis.Type = t;
            graph.AxisChange();
            graph.Refresh();
        }

        private void XaxisText_TextChanged(object sender, EventArgs e)
        {
            graph.GraphPane.XAxis.Title.Text = XaxisText.Text;
            graph.AxisChange();
            graph.Refresh();
        }

        private void YaxisText_TextChanged(object sender, EventArgs e)
        {
            graph.GraphPane.YAxis.Title.Text = YaxisText.Text;
            graph.AxisChange();
            graph.Refresh();
        }

        private void plot_Resize(object sender, EventArgs e)
        {
            try
            {
                if (this.visibleoptions)
                {
                    this.maxpos = slider.Height - 85 - slider.SplitterWidth;
                    slider.SplitterDistance = this.maxpos;
                }
                else
                {
                    this.minpos = slider.Height - slider.Panel2MinSize - slider.SplitterWidth;
                    this.slider.SplitterDistance = this.minpos;
                }
            }
            catch { ; }
        }
    }
}